package com.lc.rabbit.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.lc.rabbit.mq.product.RabbitMqProduct;
import com.lc.rabbit.service.RabbitMqService;
import com.study.common.enums.DelayedTimeEnum;
import com.study.common.enums.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/10/24 9:03
 */
@Slf4j
@Service
public class RabbitMqServiceImpl implements RabbitMqService {

    @Resource
    private RabbitMqProduct rabbitMqProduct;

    @Override
    public String sendMsg(String msg) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("time", LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd hh:mm:ss"));
            map.put("msg", msg);
            rabbitMqProduct.sendMsg(map);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (AmqpException e) {
            log.error("rabbitmq发送消息到工作队列异常：{}", e.getMessage());
            return ReturnCodeEnum.FAIL.getMsg();
        }
    }

    @Override
    public String publishMsg(String msg) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("time", LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd hh:mm:ss"));
            map.put("msg", msg);
            rabbitMqProduct.publishMsg(map);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (AmqpException e) {
            log.error("rabbitmq订阅模式发送消息到工作队列异常：{}", e.getMessage());
            return ReturnCodeEnum.FAIL.getMsg();
        }
    }

    @Override
    public String delayed1Msg(String msg) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("time", LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd hh:mm:ss"));
            map.put("msg", msg);
            rabbitMqProduct.deadMsg(map);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (Exception e) {
            log.error("rabbitmq发送消息到【延时队列】异常：{}", e.getMessage());
            return ReturnCodeEnum.FAIL.getMsg();
        }
    }

    @Override
    public String delayed2Msg(String msg) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("time", LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd hh:mm:ss"));
            map.put("msg", msg);
            rabbitMqProduct.delayedMsg(map, DelayedTimeEnum.TEN_SECOND);
            rabbitMqProduct.delayedMsg(map, DelayedTimeEnum.ONE_SECOND);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (Exception e) {
            log.error("rabbitmq发送【延时消息】到普通队列异常：{}", e.getMessage());
            return ReturnCodeEnum.FAIL.getMsg();
        }
    }

    @Override
    public String wildcardMsg(String msg) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("time", LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd hh:mm:ss"));
            map.put("msg", msg);
            rabbitMqProduct.topicMsg(map);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (Exception e) {
            log.error("【通配符模式】发送消息到队列异常：{}", e.getMessage());
            return ReturnCodeEnum.FAIL.getMsg();
        }
    }
}
